#include <stdio.h>

int max(int a,int b)
{
    return a>b?a:b;
}

void printArray(int** dp,int n)
{
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            printf("%d\t",dp[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

int main(){
    int n,m;
    int i,j,k;
    scanf("%d %d",&n,&m);
    int** road=new int*[n];
    for( i=0;i<n;i++)
    {
        road[i]=new int[n];
        road[i][i]=0;
    }
    for( i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            road[i][j]=-1;
            road[j][i]=-1;
        }
    }

    int x,y,t;
    for(i=0;i<m;i++)
    {
        scanf("%d %d %d",&x,&y,&t);
        x--;
        y--;
        road[x][y]=t;
        road[y][x]=t;
    }

    // printArray(road,n);

    for(k=0;k<n;k++)
    {
        for(i=0;i<n;i++)
        {
            if(road[i][k]>=0)
            {
                for(j=0;j<n;j++)
                {
                    if(road[k][j]>=0)
                    {
                        int num=max(road[i][k],road[k][j]);
                        if(road[i][j]==-1||road[i][j]>num)
                        {
                            road[i][j]=num;
                            road[j][i]=num;
                        }
                    }
                }
            }
        }

        // printArray(road,n);
    }

    

    int result=0;
    for( i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(road[i][j]==-1)
            {
                result=-1;
                break;
            }
            if(road[i][j]>result)
            {
                result=road[i][j];
            }
        }

        if(result==-1)
        {
            break;
        }
    }

    printf("%d\n",result);

    for( i=0;i<n;i++)
    {
        delete road[i];
    }
    delete road;
    return 0;
}